package org.ala.tcp_feign_client.spring;

import java.lang.reflect.Proxy;

import org.ala.tcp_feign_client.spring.feign.TcpFeign;
import org.ala.tcp_feign_client.spring.feign.TcpFeignInvocationHandler;
import org.ala.tcp_feign_common.config.TcpFeignConfigure;
import org.springframework.beans.factory.FactoryBean;

/**
 *
 * @author 骆毅(ala)
 * @date 2024年4月21日
 */
public class TcpFeignFactoryBean implements FactoryBean<Object> {
	
	
	/**
	 * 相关配置
	 */
	protected TcpFeignConfigure configure;
	
	/**
	 * beanName
	 */
	protected String beanName;
	/**
	 * 远程服务名称
	 */
	protected String remoteName;
	/**
	 * 远程服务host，port
	 */
	protected String hosts;
	/**
	 * 根url
	 */
	protected String path;
	/**
	 * 是否专线
	 */
	protected boolean dedicated;
	
	
	/**
	 * 被代理的接口类型
	 */
	protected Class<?> type;
	

	@Override
	public Object getObject() throws Exception {
		TcpFeign feign = build();
		TcpFeignInvocationHandler h = new TcpFeignInvocationHandler(feign, type, path);
		Object proxy = Proxy.newProxyInstance(TcpFeignFactoryBean.class.getClassLoader(), new Class<?>[] {type}, h);
		
		return proxy;
	}
	
	
	/**
	 * 构建TcpFeign
	 */
	protected TcpFeign build() {
		TcpFeign feign = new TcpFeign();
		feign.setRemoteName(remoteName);
		feign.setHosts(hosts);
		feign.setPath(path);
		feign.setDedicated(dedicated);
		feign.setType(type);
		feign.setConfigure(configure);
		feign.init();
		return feign;
	}
	
	
	@Override
	public Class<?> getObjectType() {
		return type;
	}
	
	
	public TcpFeignConfigure getConfigure() {
		return configure;
	}
	public void setConfigure(TcpFeignConfigure configure) {
		this.configure = configure;
	}
	
	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getRemoteName() {
		return remoteName;
	}

	public void setRemoteName(String remoteName) {
		this.remoteName = remoteName;
	}

	public String getHosts() {
		return hosts;
	}

	public void setHosts(String hosts) {
		this.hosts = hosts;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public boolean isDedicated() {
		return dedicated;
	}

	public void setDedicated(boolean dedicated) {
		this.dedicated = dedicated;
	}

	public Class<?> getType() {
		return type;
	}

	public void setType(Class<?> type) {
		this.type = type;
	}

}
